package com.fhz.mvvmshopjava.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.fhz.mvvmshopjava.R;
import com.fhz.mvvmshopjava.databinding.ClassifyLeftLayoutBinding;
import com.fhz.mvvmshopjava.databinding.RvLayoutBinding;
import com.fhz.mvvmshopjava.entity.Category;

import java.util.List;

/**
 * 时间:2022/6/8
 *
 * @author msfeng
 * 简述: 分类的左边的适配器
 */
public class ClassifyLeftAdapter extends RecyclerView.Adapter<ClassifyLeftAdapter.ViewHolder> {

    private List<Category> list;


    public ClassifyLeftAdapter(List<Category> list) {
        this.list = list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.classify_left_layout, null);
        ClassifyLeftLayoutBinding bind = DataBindingUtil.bind(inflate);
        return new ViewHolder(bind);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.binding.setCategory(list.get(position));
        holder.binding.executePendingBindings();

        holder.binding.classifyLeftRvName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myClick.click(holder.getAdapterPosition());
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        ClassifyLeftLayoutBinding binding;

        public ViewHolder(ClassifyLeftLayoutBinding bind) {
            super(bind.getRoot());
            this.binding = bind;
        }
    }


    public interface MyClick{
        void click(int i);
    }
    private MyClick myClick;


    public void setMyClick(MyClick myClick) {
        this.myClick = myClick;
    }
}



